<?php
namespace jy_nongfee;
defined('BY_JYA') or exit('error');
/*
	生成释放值
	
*/
class commission extends \plugin{
	/*
		生成释放值
	*/
	function create($order){
		$this->order=$order;
		pdo_update('jy_nongfee_member',array('buyprice +='=>$order['price']),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
		$member=$this->get('member',array('mid'=>$order['mid']),array('id','is_agent','fromuser','ispay'));
		if(!$member['ispay']){
			pdo_update('jy_nongfee_member',array('ispay'=>1),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
		}
		if(!$member['id']){
			$data=array('mid'=>$order['mid']);
			$this->model('member')->register($data);
			$member=$this->get('member',array('mid'=>$order['mid']),array('is_agent','fromuser'));
		}
		$this->get_zquan($order);
		$is_open=intval($this->teamsetting['isopen']);
		if(!$is_open){
			return;
		}
		$team=$this->get('team',array('mid'=>$order['mid']),array('status','fromuser'));
		$fromuser=$member['fromuser']?:$team['fromuser'];
		if($team['status']==1){
			$this->model('team')->update_agent_level($order['mid'],$order['id']);
		}
		$this->commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$this->order['mid'],
			'ordersn'=>$this->order['ordersn'],
			'teamid'=>$fromuser,
			'order_price'=>$this->order['price'],
			'commission_price'=>$this->order['price'],
			'commission_money'=>0,
			'type'=>0,
			'createtime'=>TIMESTAMP,
		);
		if($fromuser){
			//直推释放值
			$this->create_commission($fromuser);
		}
		//生成分销订单
		$this->insert('fee_order',$this->commission_order);
		return true;
	}
	//直推种植收益
	function create_area_rate($canlog){
		$member=$this->get('member',array('mid'=>$canlog['mid']));
		if($canlog['jifen']<=0){
			return false;	
		}
		$this->commission_order=array(
			'yaoid'=>$canlog['yaoid'],
			'cangid'=>$canlog['cangid'],
			'canlogid'=>$canlog['id'],
			'redfee'=>0,	
		);
		$fromuser=$member['fromuser'];
		for($i=0;$i<9;$i++){
			if(!$fromuser){
				break;
			}
			$fromuser=$this->create_nongfee($fromuser,$canlog['jifen']);
		}
	}
	function create_nongfee($fromuser,$jifen=0){
		$team=$this->get('team',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
		if($team && $team['status']){
			if(!$team['level_id']){
				return false;
			}else{
				$level=$this->get('teamlevel',array('id'=>$team['level_id'],'uniacid'=>$_SESSION['uniacid']));
				$red_per=$level['red_per'];
			}
			$redfee = intval($jifen*$red_per)/100;
			$lastfee=$redfee-$this->commission_order['redfee'];
			if($lastfee>0){
					$this->commission_order['redfee'] +=$lastfee;
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$team['mid'],
						'buymid'=>$this->commission_order['mid'],
						'yaoid'=>$this->commission_order['yaoid'],
						'cangid'=>$this->commission_order['cangid'],
						'canlogid'=>$this->commission_order['id'],
						'money'=>0,
						'jifen'=>$lastfee,
						'type'=>1,
						'createtime'=>TIMESTAMP,
						'status'=>1,
					);
					$this->insert('commission',$new_commission);
					$this->create_ping_per($new_commission,$lastfee);
			}
		}
		return $team['fromuser'];
	}
	//平一级收益
	function create_ping_per($new_commission,$fee=0){
		$member=$this->get('member',array('mid'=>$new_commission['mid']));
		if($fee<=0){
			return false;	
		}
		$this->ping_order=array(
			'yaoid'=>$new_commission['yaoid'],
			'cangid'=>$new_commission['cangid'],
			'canlogid'=>$new_commission['canlogid'],
			'pingfee'=>0,	
		);
		$fromuser=$member['fromuser'];
		for($i=0;$i<9;$i++){
			if(!$fromuser){
				break;
			}
			$fromuser=$this->create_pingfee($fromuser,$fee);
		}
	}
	function create_pingfee($fromuser,$fee=0){
		$team=$this->get('team',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
		if($team && $team['status']){
			if(!$team['level_id']){
				return false;
			}else{
				$level=$this->get('teamlevel',array('id'=>$team['level_id'],'uniacid'=>$_SESSION['uniacid']));
				$ping_per=$level['ping_per'];
			}
			$pingfee=0;
			if($this->ping_order['pingfee']<=0 && $ping_per>0){
				$pingfee = intval($fee*$ping_per)/100;
			}
			if($pingfee>0){
					$this->ping_order['pingfee'] +=$pingfee;
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$team['mid'],
						'buymid'=>$this->ping_order['mid'],
						'yaoid'=>$this->ping_order['yaoid'],
						'cangid'=>$this->ping_order['cangid'],
						'canlogid'=>$this->ping_order['id'],
						'money'=>0,
						'jifen'=>$pingfee,
						'type'=>2,
						'createtime'=>TIMESTAMP,
						'status'=>1,
					);
					$this->insert('commission',$new_commission);
			}
		}
		return $team['fromuser'];
	}
	/*
		
	*/
	function create_commission($fromuser,$commission_level=1){
		$team=$this->get('team',array('mid'=>$fromuser));
		if($team && $team['status']){
			//相应会员存在且是分销商且状态正常			
			if(!$team['level_id']){
				return false;
			}else{
				$level=$this->get('teamlevel',array('id'=>$team['level_id'],'uniacid'=>$_SESSION['uniacid']));
				$level_id=$level['id'];
				$fee_per=$level['fee_per'];
			}
			$money=0;
			$order_price=$this->commission_order['commission_price'];
			$remark=array(
				'level_id'=>$level_id,
				'commission_level'=>$commission_level,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
			);
			
			//先检查是否存在商品独立佣金规则，如果存在则对订单相应商品部分金额单独计算佣金
			$total_lastcostprice=0;
			if($order_price>0){
				$money = intval($order_price*$fee_per)/100;
				$remark['order_commission']=array('order_price'=>$order_price,'commission_per'=>$fee_per,'commission_money'=>$money);
			}
			$remark['commission_money']=$money;
			if($money>0){
					$this->commission_order['commission_money']+=$money;
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$team['mid'],
						'buymid'=>$this->commission_order['mid'],
						'level'=>$commission_level,
						'orderid'=>$this->order['id'],
						'ordersn'=>$this->order['ordersn'],
						'jifen'=>$money,
						'type'=>0,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>1,
					);
					$this->insert('commission',$new_commission);
					$this->create_ping_per($new_commission,$money);
			}
			$this->model('team')->update_agent_level($team['mid']);
		}
		//如果他的上级是我
		$ta_fromuser=$this->getcolumn('team',array('mid'=>$team['fromuser']),'fromuser');
		if($ta_fromuser==$fromuser){
			return false;
		}
		return $team['fromuser'];
	}
	//购物送种植券
	function get_zquan($order=array()){
		$quan_set=$this->setting['quan_set'];
		paixu($quan_set,'price','desc');
		foreach($quan_set as $item){
			if($order['price']>=$item['price']){
				pdo_update('jy_nongfee_member',array('zquan +='=>intval($item['num'])),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
				$quanlog=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$order['mid'],
						'orderid'=>$order['id'],
						'num'=>$item['num'],
						'fee'=>$order['price'],
						'type'=>0,
						'createtime'=>TIMESTAMP,
				);
				$this->insert('quanlog',$quanlog);
				break;
			}
		}
		return true;
	}
	//加权分红
	public function get_store_per(){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$isget=pdo_get('jy_nongfee_commission',array('createtime >'=>$daytime,'type'=>8,'uniacid'=>$_SESSION['uniacid'],),'id');
		if($isget['id']){
			return false;
		}
		$levelids=$this->getall('teamlevel',array('store_per >'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','store_per'),'id');
		if(!$levelids){
			return false;
		}
		$store_per=$levelids[0]['store_per'];
		$levelids=array_keys($levelids);
		$teams=$this->getall('team',array('level_id'=>$levelids,'uniacid'=>$_SESSION['uniacid']));
		$num=count($teams);
		if(!$num){
			return false;
		}
		//获取加权分红
		$params=array('uniacid'=>$_SESSION['uniacid']);
		$params['createtime between']=array($daytime-24*3600,$daytime);
		$money=pdo_getsum('jy_nongfee_canlog',$params,'jifen');
		if($money<=0 || $money<=0.01){
			return false;
		}
		$money=$money*$store_per/100;
		$onefee=$money/$num;
		foreach($teams as $item){
			$new_commission=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$item['mid'],
				'jifen'=>$onefee,
				'type'=>8,
				'remark'=>'加权分红',
				'createtime'=>TIMESTAMP,
				'status'=>1,
			);
			$this->insert('commission',$new_commission);
		}
		return true;
	}
	/*
		订单完成时，把佣金状态由初始的订单待完成升级为待结算
	*/
	public function order_complete($order){
		//无
	}
	/*
		创建释放结算单
	*/
	public function create_balance($mid,$post=array()){
		if(!$mid){
			return false;
		}
		//获取可结算佣金
		$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'status'=>1);
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$params['createtime between']=array($daytime-24*3600,$daytime);
		$money=pdo_getsum('jy_nongfee_commission',$params,'jifen');
		if($money<=0 || $money<=0.01){
			return false;
		}
		
		$this->teamsetting=$this->plugin_setting('teamsetting');
		$commission_num=$this->count('commission',$params,'id');
		$sf_hour=$this->teamsetting['sf_hour']?:24;
		$end_time=TIMESTAMP+$sf_hour*3600;
		if($end_time>$daytime+24*3600){
			$end_time=$daytime+24*3600;
		}
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'commission_num'=>$commission_num,
			'money'=>$money,
			'lastmoney'=>$money,
			'createtime'=>TIMESTAMP,
			'end_time'=>$end_time,
			'status'=>0,
		);
		$this->insert('balance',$new_balance);
		$balance_id=pdo_insertid();
		$update=array(
			'status'=>2,
			'balancesn'=>$new_balance['balancesn'],
		);
		$this->update('commission',$update,$params);
		$this->update('member',array('sffee +='=>$money),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		return $balance_id;
	}
	public function credit4tocredit3($fee,$balance=array()){
		//无
		pdo_update('jy_nongfee_balance',array('lastmoney -='=>$fee),array('id'=>$balance['id'],'uniacid'=>$_SESSION['uniacid']));
		$this->update('member',array('sffee -='=>$fee),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$balance['mid']));
		$this->add_balancelog($fee,1,$balance['mid']);
	}
	public function up_endtime($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$balances=pdo_getall('jy_nongfee_balance',array('iskou'=>0,'end_time <'=>TIMESTAMP,'lastmoney >'=>0,'status'=>0,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if(count($balances)){
			$member=model('core/member')->detail($mid,array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
			$credit4=$member['credit4'];
			$teamsetting=$this->plugin_setting('teamsetting');
			$areasetting=$this->plugin_setting('areasetting');
			$sf_rate=$teamsetting['sf_rate']?:$areasetting['sf_rate'];
			$sf_rate=$sf_rate?:1;
			$canlastfee=$credit4/$sf_rate;
		}
		foreach($balances as $item){
			if($canlastfee < $item['lastmoney']){
				$koufee=$item['lastmoney']-$canlastfee;
				pdo_update('jy_nongfee_balance',array('iskou'=>1,'lastmoney -='=>$koufee),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				$this->update('member',array('sffee -='=>$koufee,'dongfee +='=>$koufee),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$balance['mid']));
				$this->add_balancelog($koufee,2,$item['mid']);
			}
		}
		pdo_update('jy_nongfee_balance',array('iskou'=>1),array('iskou'=>0,'end_time <'=>TIMESTAMP,'status'=>0,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function add_balancelog($fee,$status=0,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'money'=>$fee,
			'status'=>$status,
			'createtime'=>TIMESTAMP,
		);
		$this->insert('balance',$data);
		return true;
	}
}